home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / boot / snap_v2_0.lha / Snap / Misc.c < prev    next >
C/C++ Source or Header  |  1995-09-13  |  5KB  |  268 lines

  1. /* Auto: make "CCEXTRA=-wq -qf"
  2.  */
  3.  
  4. #include "Snap.h"
  5.  
  6. #include "proto/Misc.h"
  7.  
  8. #define SCREENTOP (screen->TopEdge << ((screen->ViewPort.Modes & LACE)? 0: 1))
  9.  
  10. IMPORT struct SnapRsrc *SnapRsrc;
  11. IMPORT struct IntuitionBase *IntuitionBase;
  12. IMPORT struct Library *DiskfontBase;
  13.  
  14. struct Screen *WhichScreen()
  15. {
  16.     REGISTER struct Screen *screen;
  17.     REGISTER ULONG lock;
  18.  
  19.     lock = LockIBase(0);
  20.     screen = IntuitionBase->FirstScreen;
  21.     while (screen && IntuitionBase->MouseY < SCREENTOP)
  22.     {
  23.         screen = screen->NextScreen;
  24.     }
  25.     if (screen == NULL)
  26.     {            /* Shouldn't happen */
  27.         screen = IntuitionBase->ActiveScreen;
  28.     }
  29.     UnlockIBase(lock);
  30.     return screen;
  31. }
  32.  
  33. struct Window *WhichWindow(screen)
  34.     struct Screen *screen;
  35. {
  36.     struct Layer *layer;
  37.  
  38.     layer = (struct Layer *)WhichLayer(&screen->LayerInfo,
  39.                   (LONG) screen->MouseX, (LONG) screen->MouseY);
  40.     if (layer)
  41.     {
  42.         return (struct Window *)layer->Window;
  43.     }
  44.     else
  45.     {
  46.         return NULL;
  47.     }
  48. }
  49.  
  50.  
  51. static VOID FreePlanes( struct BitMap *bm, LONG width, LONG height)
  52. {
  53.     SHORT i;
  54.  
  55.     for (i = 0; i < bm->Depth; i++)
  56.     {
  57.         if (bm->Planes[i])
  58.         {
  59.             FreeRaster(bm->Planes[i], width, height);
  60.             bm->Planes[ i] = NULL;
  61.         }
  62.     }
  63. }
  64.  
  65. static BOOL AllocPlanes( struct BitMap *bm, LONG width, LONG height)
  66. {
  67.     WORD i;
  68.  
  69.     for (i = 0; i < bm->Depth; i++)
  70.     {
  71.         bm->Planes[i] = NULL;
  72.     }
  73.     for (i = 0; i < bm->Depth; i++)
  74.     {
  75.         if ( !( bm->Planes[i] = AllocRaster(width, height)))
  76.         {
  77.             FreePlanes( bm, width, height);
  78.             return FALSE;
  79.         }
  80.         BltClear((char *)bm->Planes[i], RASSIZE(width, height), NULL);
  81.     }
  82.     return TRUE;
  83. }
  84.  
  85. void Snp_FreeBitMap( struct BitMap *BitMap, ULONG Width, ULONG Height)
  86. {
  87.     if ( !BitMap)
  88.         return;
  89.  
  90.     if ( IntuitionBase->LibNode.lib_Version < 39)
  91.     {
  92.         FreePlanes( BitMap, Width, Height);
  93.         FreeVec( BitMap);
  94.  
  95.         return;
  96.     }
  97.     else
  98.     {
  99.         FreeBitMap( BitMap);
  100.     }
  101.  
  102. }
  103.  
  104. struct BitMap *Snp_AllocBitMap( ULONG SizeX, ULONG SizeY, ULONG Depth, ULONG Flags, struct BitMap *FriendBitMap)
  105. {
  106.     if ( IntuitionBase->LibNode.lib_Version < 39)
  107.     {
  108.         struct BitMap *BitMap;
  109.  
  110.         if ( BitMap = AllocVec( sizeof( struct BitMap), MEMF_ANY))
  111.         {
  112.             InitBitMap( BitMap, Depth, SizeX, SizeY);
  113.             if ( AllocPlanes( BitMap, SizeX, SizeY))
  114.             {
  115.                 return BitMap;
  116.             }
  117.             FreeVec( BitMap);
  118.             return NULL;
  119.         }
  120.     }
  121.     else
  122.     {
  123.         return AllocBitMap( SizeX, SizeY, Depth, Flags, FriendBitMap);
  124.     }
  125. }
  126.  
  127. VOID CacheWindow(struct Window * Win,
  128.          LONG xoff, LONG yoff,
  129.          SHORT fw, SHORT fh,
  130.          struct TextFont * font)
  131. {
  132.     struct CacheWindow *cw;
  133.  
  134.     if (!(cw = Create(CacheWindow)))
  135.     {
  136.         return;
  137.     }
  138.     cw->Window = Win;
  139.     cw->xoff = xoff;
  140.     cw->yoff = yoff;
  141.     cw->fw = fw;
  142.     cw->fh = fh;
  143.     cw->font = font;
  144.     AddHead((struct List *)&SnapRsrc->CachedWindows, (struct Node *)cw);
  145.     if (SnapRsrc->CacheSize > 0)
  146.     {
  147.         --SnapRsrc->CacheSize;
  148.     }
  149.     else
  150.     {
  151.         cw = (struct CacheWindow *)RemTail((struct List *)&SnapRsrc->CachedWindows);
  152.         Kill(cw);
  153.     }
  154. }
  155.  
  156. VOID CacheSync(Font)
  157.     struct TextFont *Font;
  158. {
  159.     struct CacheWindow *cw;
  160.     struct CacheWindow *safe;
  161.  
  162.     SAFEMAPLIST(&SnapRsrc->CachedWindows, struct CacheWindow *, cw, safe)
  163.     {
  164.         if (cw->font == SnapRsrc->AlternateFont)
  165.         {
  166.             Remove((struct Node *)cw);
  167.             Kill(cw);
  168.         }
  169.     }
  170. }
  171.  
  172. struct CacheWindow *GetCachedWindow(Screen, Win)
  173.     struct Screen *Screen;
  174.     struct Window *Win;
  175. {
  176.     struct CacheWindow *cw;
  177.  
  178.     MAPLIST(&SnapRsrc->CachedWindows, struct CacheWindow *, cw)
  179.     {
  180.         if (cw->Window == Win)
  181.         {
  182.             LONG LockVal = LockIBase(0L);
  183.             REGISTER struct Window *w = Screen->FirstWindow;
  184.  
  185.             while (w && w != Win)
  186.             {
  187.                 w = w->NextWindow;
  188.             }
  189.             UnlockIBase(LockVal);
  190.             if (!w)
  191.             {
  192.                 return NULL;
  193.             }
  194.             Remove((struct Node *)cw);
  195.             AddHead((struct List *)&SnapRsrc->CachedWindows, (struct Node *)cw);
  196.             return cw;
  197.         }
  198.     }
  199.     return NULL;
  200. }
  201.  
  202. struct TextFont *SmartOpenFont(ta)
  203.     struct TextAttr *ta;
  204. {
  205.     struct TextFont *MemF;
  206.     struct TextFont *DskF = NULL;
  207.  
  208.     if ((MemF = OpenFont(ta)) && ta->ta_YSize == MemF->tf_YSize)
  209.     {
  210.         return MemF;
  211.     }
  212.  
  213.     if (DiskfontBase)
  214.     {
  215.         DskF = OpenDiskFont(ta);
  216.     }
  217.  
  218.     if (!MemF)
  219.     {
  220.         return DskF;
  221.     }
  222.     if (!DskF)
  223.     {
  224.         return MemF;
  225.     }
  226.     if (abs(ta->ta_YSize - MemF->tf_YSize) <=
  227.         abs(ta->ta_YSize - DskF->tf_YSize))
  228.     {
  229.         CloseFont(DskF);
  230.         return MemF;
  231.     }
  232.     CloseFont(MemF);
  233.     return DskF;
  234. }
  235.  
  236.  
  237. /*
  238.     00000RGB
  239.     0000000R
  240.     000000R0
  241. */
  242.  
  243. UBYTE RGB4ToRR( ULONG RGB4)
  244. {
  245.     UBYTE R = ( RGB4 >> 8) & 0x0F;
  246.     return (UBYTE)( R | ( R << 4));
  247. }
  248. UBYTE RGB4ToGG( ULONG RGB4)
  249. {
  250.     UBYTE R = ( RGB4 >> 4) & 0x0F;
  251.     return (UBYTE)( R | ( R << 4));
  252. }
  253. UBYTE RGB4ToBB( ULONG RGB4)
  254. {
  255.     UBYTE R = ( RGB4 >> 0) & 0x0F;
  256.     return (UBYTE)( R | ( R << 4));
  257. }
  258.  
  259. UBYTE RGB32ToRGB8( ULONG RGB32)
  260. {
  261.     return (UBYTE)( RGB32 >> 24);
  262. }
  263.  
  264. ULONG RGB8ToRGB32( UBYTE RGB8)
  265. {
  266.     return ( ( (ULONG)RGB8 << 24) | ( (ULONG)RGB8 << 16) | ( (ULONG)RGB8 << 8) | ( (ULONG)RGB8 << 0) );
  267. }
  268.